package com.bawei.module_advice.ui


import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.lib_common.base.BaseFragment
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_advice.R
import com.bawei.module_advice.adapter.AdviceAdapter
import com.bawei.module_advice.databinding.FragmentAdviceBinding
import com.bawei.module_advice.http.AdviceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AdviceFragment : BaseFragment<BaseViewModel,FragmentAdviceBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_advice
    private val mViewModel: AdviceViewModel by viewModels()
    //1


    val mAdapter : AdviceAdapter by lazy {
        AdviceAdapter()
    }

    override fun initView() {
        super.initView()
        initAdapter()
    }

    //获取数据
    override fun initData() {
        mViewModel.getAdvice(1,20)
    }
    //实现网络接口
    override fun initListener() {
        mViewModel.noticeState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    Log.i("GZY", "initListener: "+it.message)
                }
                Loading -> {}
                Success -> {
                    mAdapter.setData(mViewModel.adviceList)
                    Log.i("GZY", "initListener: "+mViewModel.adviceList.size)
                }
            }
        }
    }

    private fun initAdapter(){
        mAdapter.setData(mViewModel.adviceList)

        mBinding.advRv.layoutManager=LinearLayoutManager(context)
        mBinding.advRv.adapter=mAdapter


    }

}